// pages/we/mymission/mymission.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    modalName: '',
    mission: '',
    missions: [],
    score:{},
    price: 0
  },
  showModal() {
    this.setData({
      modalName: "addmission"
    })
  },
  hideModal() {
    this.setData({
      modalName: ''
    })
  },
  getmission(e) {
    this.setData({
      mission: e.detail.value
    })
  },
  getprice(e) {
    this.setData({
      price: e.detail.value
    })
  },
  commit() {
    wx.cloud.database().collection('mission').add({
      data: {
        lover_openid: wx.getStorageSync('lover')._openid,
        mission: this.data.mission,
        price: parseInt(this.data.price),
        complete: false
      }
    }).then(res => {
      this.hideModal()
      wx.showToast({
        title: '提交成功',
      })
    })
  },
  complete(e) {
    wx.showLoading({
      title: '请稍后'
    })
    this.data.missions[e.target.dataset.index].complete = true
    wx.cloud.callFunction({
      name: 'mission',
      data: {
        mission: this.data.missions[e.target.dataset.index]
      }
    }).then(res => {
      this.data.score.score += this.data.missions[e.target.dataset.index].price
      wx.cloud.database().collection('score').doc(this.data.score._id).update({
        data: {
          score: this.data.score.score
        }
      }).then(res=>{
        this.setData({
          missions: this.data.missions,
          score: this.data.score
        })
        wx.hideLoading()
        wx.showToast({
          title: '完成',
        })
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.cloud.database().collection('score').where({
      _openid: wx.getStorageSync('user')._openid
    })
      .get()
      .then(res => {
        if (res.data.length == 0) {
          wx.cloud.database().collection('score').add({
            data: {
              score: parseInt(0)
            }
          }).then(res => {
            wx.cloud.database().collection('score').where({
              _openid: wx.getStorageSync('user')._openid
            }).get().then(res => {
              this.setData({
                score: res.data[0]
              })
            })
          })
        } else {
          wx.cloud.database().collection('score').where({
            _openid: wx.getStorageSync('user')._openid
          }).get().then(res => {
            this.setData({
              score: res.data[0]
            })
          })
        }
      })
    wx.cloud.database().collection('mission').where({
      lover_openid: wx.getStorageSync('user')._openid
    }).get().then(res => {
      this.setData({
        missions: res.data
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})